using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using  server.status_db;
[Authorize]
public class JksqController : ODataController
{
    private status_oneContext _db;
    public JksqController(status_oneContext context)
    {
        _db = context;

    }
    private bool MCExists(Guid key)
    {
        return _db.PG_B_XNJKSKQK.Any(p => p.ID == key);
    }
    [HttpPut]
    public async Task<IActionResult> Put([FromODataUri] Guid key, [FromBody]PG_B_XNJKSKQK update)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        if (key != update.ID)
        {
            return BadRequest();
        }
        _db.Entry(update).State = EntityState.Modified;
        try
        {
            await _db.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!MCExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }
        return Updated(update);
    }
    public async Task<IActionResult> Delete([FromODataUri] Guid key)
    {
        var mc = await _db.PG_B_XNJKSKQK.FindAsync(key);
        if (mc == null)
        {
            return NotFound();
        }
        _db.PG_B_XNJKSKQK.Remove(mc);
        await _db.SaveChangesAsync();
        return NoContent();
    }
    [HttpPost]
    public async Task<IActionResult> Post([FromBody] PG_B_XNJKSKQK model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        _db.PG_B_XNJKSKQK.Add(model);
        await _db.SaveChangesAsync();

        return Created(model);
    }
     [HttpGet("/api/Jksq/AuditAllData")]
    public IActionResult AuditAllData(String GH,String XM,String status,String XXID, String CJPC)
    {
        
        _db.Database.ExecuteSqlCommand($"update PG_B_XNJKSKQK set 审核卡号={GH},审核姓名={XM},审核状态={status},审核时间= GETDATE() where XXID={XXID} and CJPC={CJPC}");
         return NoContent();
    }
     [HttpGet("/api/Jksq/RemoveAllData")]
   public IActionResult RemoveAllData(String GH,String XM,String XXID, String CJPC){
         _db.Database.ExecuteSqlCommand($"update PG_B_XNJKSKQK set 删除卡号={GH},删除姓名={XM},是否删除=1,删除时间= GETDATE() where XXID={XXID}  and  CJPC={CJPC}");
         return NoContent();
    }
    [EnableQuery]
    public IActionResult Get()
    {
        return Ok(_db.PG_B_XNJKSKQK);
    }
    //
}